/**
 * 运行或打包后进行错别字修改
 * @param source other
 * targetList 错别字列表 s为有错的错别字 t为纠正的
 * @returns {*}
 */
module.exports = function(source) {
  let targetList = [
    {s: '帐号', t: '账号'},
    {s: '登陆', t: '登录'},
    {s: '帐套', t: '账套'},
    {s: '预定', t: '预订'},
    {s: '发票贷', t: '发票袋'}
  ];
  targetList.map(item => {
    item.u = toUnicode(item.s);
    item.p = toUnicode(item.t);
    return item;
  });
  let reg = new RegExp(`(${targetList.map(item => `(${item.s})`).join('|')})`,'iug');
  source = source.replace(reg, (target, $1) => {
    targetList.map(item => {
      $1.indexOf(item.s) > -1 && (target = item.t);
    });
    return target;
  });
  targetList.map(item => {
    source = globalReplace(source,item.u,item.p);
  });
  return source;
};

function toUnicode(data) {
  var str = '';
  var i = 0;
  for (i; i < data.length; i++) {
    str += "\\u" + data.charCodeAt(i).toString(16).toUpperCase();
  }
  return str;
}

function globalReplace(content,str,target){
  if(content.indexOf(str) > -1){
    content = content.replace(str,target);
    return globalReplace(content,str,target);
  }else{
    return content;
  }
};
